package com.project.light.controller;


import com.project.light.dto.*;
import com.project.light.util.HttpUtil;
import com.project.light.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/lighting-rest/api/v1/control/")
public class ControlController {

    @Resource
    ConfigDto configDto;

    @PostMapping("setStationLightingParams/post")
    public RestResult setStationLightingParams(@RequestHeader(value = "authorization") String token, @RequestBody LightingParam param) {
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/control/SetStationLightingParams";
            HttpUtil httpUtil = new HttpUtil(url, token, configDto.getTimeout());
            httpUtil.postUrl(param);
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok();
    }

    @PostMapping("setGroupLightingParams/post")
    public RestResult setGroupLightingParams(@RequestHeader(value = "authorization") String token, @RequestBody GroupLightingParam param) {
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/control/SetGroupLightingParams";
            HttpUtil httpUtil = new HttpUtil(url, token, configDto.getTimeout());
            httpUtil.postUrl(param);
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok();
    }

    @PostMapping("playDynamicProgram/post")
    public RestResult playDynamicProgram(@RequestHeader(value = "authorization") String token, @RequestBody DynamicProgramParam param) {
        DynamicProgramResult result;
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/control/PlayDynamicProgram";
            HttpUtil httpUtil = new HttpUtil(url, token, configDto.getTimeout());
            result = (DynamicProgramResult)httpUtil.postUrl(param);
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok(result);
    }

    @PostMapping("getDynamicProgramStatus/post")
    public RestResult getDynamicProgramStatus(@RequestHeader(value = "authorization") String token, @RequestBody DynamicProgramParam param) {
        DynamicProgramStatusResult result;
        try {
            String url = param.getHost() + "/lighting-rest/api/v1/control/GetDynamicProgramStatus?sysId=" + param.getSysId();
            HttpUtil httpUtil = new HttpUtil(url, token, configDto.getTimeout());
            result = (DynamicProgramStatusResult) httpUtil.getUrl();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
        return RestResult.ok(result);
    }
}
